/**
 \* Created with IntelliJ IDEA.
 \* @author: walter_long
 \* @date: 2019/10/23
 \* @filename: survey
 \* Description:
 \*/
import API from "../../api/api_survey";

const state = {
    pageParam: {
        name: "",
        limit: 10,
        page: 1
    },
    pageData: {
        currentPage: 1,
        pageSize: 5,
        pageSizes: [5],
        total: 0,
        rows: []
    },
    /**
     * 新增survey临时数据
     */
    addSurvey: {
        name: "",
        tag: ""
    }
};

const getters = {
    pageData: state => {
        return state.pageData
    },
    pageParam: state => {
        return state.pageParam
    },
    addSurvey: state => {
        return state.addSurvey;
    }
};

const actions = {
    /**
     * 分页列出问卷
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<any>}
     */
    listSurveysByPageAct({state, commit}, params) {
        let param;
        if (!params) {
            param = state.pageParam
        } else {
            param = {
                limit: params.limit ? params.limit : state.pageParam.limit,
                page: params.page ? params.page : state.pageParam.page,
                name: params.name ? params.name : state.pageParam.name
            }
        }
        commit('setPageParam', param);
        return new Promise((resolve, reject) => {
            API.listSurveysByPage(param).then(result => {
                if (!result) {
                    reject(result);
                    return;
                }
                result.pageSizes = [5];
                commit('setPageData', result);
                resolve(result)
            }, error => {
                reject(error)
            }).catch(err => {
                reject(err)
            })
        })
    },
    /**
     * 刷新分页数据
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<any>}
     */
    resetPageAct({state, commit}, params) {
        let param = {
            name: "",
            limit: 10,
            page: 1
        };
        commit('setPageParam', param);
        return new Promise((resolve, reject) => {
            actions.listSurveysByPageAct({state, commit}).then(res => {
                resolve(res);
            }).catch(e => {
                reject(e);
            })
        });
    },
    /**
     * 删除一个问卷
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<any>}
     */
    restartSurveyAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.restartSurvey(params).then(result => {
                resolve(result)
            }, error => {
                reject(error)
            }).catch(err => {
                reject(err)
            })
        })
    },
    /**
     * 发布一个问卷
     * @param state
     * @param commit
     * @param params
     * @returns {Promise<any>}
     */
    stopSurveyAct({state, commit}, params) {
        return new Promise((resolve, reject) => {
            API.stopSurvey(params).then(result => {
                resolve(result)
            }, error => {
                reject(error)
            }).catch(err => {
                reject(err)
            })
        })
    },
    setAddSurveyAct({state, commit}, params) {
        commit("setAddSurvey", params);
    }
};

const mutations = {
    setPageData(state, entity) {
        state.pageData = entity
    },
    setPageParam(state, entity) {
        state.pageParam = entity
    },
    setAddSurvey(state, entity) {
        state.addSurvey = entity
    }
};

export default {
    state,
    getters,
    actions,
    mutations,
    namespaced: true
}
